<template>
  <div class="user-info">
    <!-- 个人信息 -->
    <el-form ref="userForm" label-width="220px" :rules="rules" :model="userInfo">
      <!--手机 -->

      <el-form-item label="手机">
        <el-input v-model="userInfo.mobile" style="width:220px" disabled />
      </el-form-item>

      <!-- 工号 入职时间 -->

      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="userInfo.timeOfEntry"
          type="date"
          class="inputW"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>

      <!-- 员工照片 -->

      <el-form-item label="员工头像">
        <!-- 放置上传图片 -->
        <!-- <img v-if="userInfo.staffPhoto" :src="userInfo.staffPhoto" style="width:200px">
        <img v-else src="@/assets/common/伊织萌01.png" style="width:200px"> -->
        <UploadImg v-model="userInfo.staffPhoto" />
      </el-form-item>

      <!-- 保存个人信息 -->
      <el-form-item>
        <el-button type="primary" @click="hClick">保存更新</el-button>
        <el-button @click="$router.back()">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { getUserByID, saveUserDetailById } from '@/api/user'
export default {
  data() {
    return {
      userInfo: {
        userId: this.$route.query.id,
        mobile: '',
        timeOfEntry: '',
        staffPhoto: ''
      },
      rules: {
        timeOfEntry: [{ required: true, trigger: 'blur' }]
      }
    }
  },
  created() {
    this.setUserByID()
  },
  methods: {
    // 默认刷新页面
    async setUserByID() {
      try {
        const res = await getUserByID(this.userInfo.userId)
        // console.log('getUserByID', res)
        this.userInfo = res.data.data
        // 保存数据
      } catch (err) {
        console.log('getUserByID', err)
      }
    },
    // 保存更新
    hClick() {
      this.$refs.userForm.validate(async(valid) => {
        if (!valid) return // 如果表单验证不通过，直接return
        // 通过的代码
        try {
          await saveUserDetailById(this.userInfo)
          // console.log(res)
          // this.userInfo = res.data.data
          this.$message.success('更新个人信息成功')
          this.$router.back()
        } catch (error) {
          console.log(error)
        }
      })
    }
  }

}
</script>

<style scoped lang="scss">
  .user-info{
    padding-top:20px;
  }
</style>
